//
//  ModelManager.swift
//  SQliteData
//
//  Created by Henry LIU on 16/12/2014.
//  Copyright (c) 2014 Henry LIU. All rights reserved.
//

import UIKit



let sharedInstance = ModelManager();

class ModelManager: NSObject {
    var database: FMDatabase? = nil
    
    class var instance: ModelManager{
        sharedInstance.database = FMDatabase(path: Utils.getPath("sql.sqlite"));
        var path = Utils.getPath("sql.sqlite");
        println("path: \(path)")
        return sharedInstance;
    }
    
    func addStudentData(studentInfo: StudentInfo) ->Bool{
        sharedInstance.database!.open();
        let isInserted = sharedInstance.database!.executeUpdate("INSERT INTO StudentInfo (student_rollno,student_name) VALUES(?,?)", withArgumentsInArray: [studentInfo.studentRollNo,studentInfo.studentName])
        sharedInstance.database!.close();
        return isInserted;
        
    }
    
    func updateStudentData(studentInfo: StudentInfo) ->Bool{
        sharedInstance.database!.open()
        let isUpdated = sharedInstance.database!.executeUpdate("UPDATE StudentInfo set student_name=? WHERE student_rollno=?", withArgumentsInArray: [studentInfo.studentName, studentInfo.studentRollNo])
        sharedInstance.database!.close()
        return isUpdated
    }
    
    func deleteStudentData(studentInfo: StudentInfo) ->Bool{
        sharedInstance.database!.open()
        let isDeleted = sharedInstance.database!.executeUpdate("DELETE FROM StudentInfo WHERE student_rollno=?", withArgumentsInArray: [studentInfo.studentRollNo])
        sharedInstance.database!.close()
        return isDeleted
    }
    
    func getAllStudnentData(){
        sharedInstance.database!.open()
        var resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM StudentInfo", withArgumentsInArray: nil)
        var rollNoColumn: String = "student_rollno"
        var nameColumn: String = "student_name"
        if(resultSet != nil){
            while resultSet.next(){
                println("roll no.: \(resultSet.stringForColumn(rollNoColumn))")
                println("name: \(resultSet.stringForColumn(nameColumn))")
            }
        }
        sharedInstance.database!.close()
    }
    
}
